package com.manor.paradise.fragment.npcshop

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.Constants
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.adapter.npcshop.GiftsAdapter
import com.manor.paradise.bean.AddressBean
import com.manor.paradise.bean.GiftsBean
import com.manor.paradise.dialog.GiftPopup
import com.manor.paradise.dialog.TelephoneChargesPopup
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.fragment_gifts.*

/**
 * 兑换礼品
 */
class ExchangeGiftsFragment : Fragment() {
    var mCurView: View? = null

    internal lateinit var mAdapter: GiftsAdapter
    internal var pageNum = 1


    companion object {
        fun newInstance(): ExchangeGiftsFragment {
            val args = Bundle()
            val fragment = ExchangeGiftsFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        mCurView = inflater.inflate(R.layout.fragment_gifts, container, false)
        return mCurView
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            pageNum = 1
            setData()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val layoutManager = GridLayoutManager(context, 3)
        rcv_list.layoutManager = layoutManager
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
    }

    fun setData() {
        ElvesHttpUtil.getGiftList(pageNum, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var giftsBean: GiftsBean = GsonUtil.gsonToBean<GiftsBean>(info, GiftsBean::class.java)
                KLog.e("json", giftsBean);
                if (pageNum == 1) {
                    mAdapter = GiftsAdapter(R.layout.item_npc_shop, giftsBean.records)
                    mAdapter.setOnItemClickListener(itemChildClickListener)
                    rcv_list.setAdapter(mAdapter)
                    refreshLayout.finishRefresh()
                } else {
                    mAdapter.addData(giftsBean.records)
                    refreshLayout.finishLoadMore()
                }

            }

            override fun onError() {
                super.onError()

            }
        })
    }

    private var giftPopup: GiftPopup? = null

    private val itemChildClickListener = OnItemClickListener { adapter, view, position ->
        if (ClickUtil.canClick(500)) {
            if (mAdapter.getItem(position).isBlogistics) {

                giftPopup = activity?.let { GiftPopup(it, mAdapter.getItem(position)) }

                XPopup.Builder(context)
                        .hasShadowBg(true)
                        .dismissOnTouchOutside(true)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                        .asCustom(giftPopup)
                        .show()
            } else {
                val telephoneChargesPopup = context?.let { TelephoneChargesPopup(it, mAdapter.getItem(position)) }

                XPopup.Builder(context)
                        .hasShadowBg(true)
                        .dismissOnTouchOutside(true)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                        .asCustom(telephoneChargesPopup)
                        .show()
            }
        }

    }

    fun setGiftPop(data: AddressBean.RecordsBean) {
        giftPopup?.run {
            giftPopup?.setCity(data)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null != data) {
            if (resultCode == 101) {
                if (null != data.getSerializableExtra(Constants.GIFT_CITY)) {

                }

            }
        }
    }

    override fun onDestroy() {

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_NPC_SHOP_LIST)
        super.onDestroy()
    }
}